package com.qianji.petmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.Query;
import com.qianji.petmall.product.dao.BrandDao;
import com.qianji.petmall.product.dao.CategoryBrandRelationDao;
import com.qianji.petmall.product.dao.CategoryDao;
import com.qianji.petmall.product.entity.BrandEntity;
import com.qianji.petmall.product.entity.CategoryBrandRelationEntity;
import com.qianji.petmall.product.entity.CategoryEntity;
import com.qianji.petmall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> queryList(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        List<CategoryBrandRelationEntity> id = categoryBrandRelationDao.selectList(wrapper.eq("brand_id", catId));
        return id;
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //1、查询出品牌名和分类名
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        categoryBrandRelationDao.insert(categoryBrandRelation);
    }

    @Override
    public List<BrandEntity> seleteByBrandId(Long catId) {
        List<CategoryBrandRelationEntity> brand_id = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandEntity> collect = brand_id.stream().map(item -> {
            BrandEntity entity = brandDao.selectById(item.getBrandId());
            return entity;
        }).collect(Collectors.toList());
        return collect;
    }

    //到这
    public void updateCategery(Long catId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogName(name);
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        baseMapper.update(entity,wrapper.eq("catelog_id",catId));
    }
}